{extend name="iframe" /}

{block name="body"}
<style>
    .layui-upload-img {
        width: 92px;
        height: 92px;
        margin: 0 10px 10px 0;
    }
</style>
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <form>
        <div class="layui-form-item">
            <label class="layui-form-label">行业类型</label>
            <div class="layui-input-inline">
                <select name="parent_typeid" lay-filter="parent_typeid" id="parent_typeid">
                    <option value="0">请选择</option>
                    {foreach name="parent_typelist" item="vo"}
                    <option value="{$vo.id}" {if condition="$data.parent_typeid eq $vo.id"}selected{/if} >{$vo.title}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="typeid" id="typeid">
                    <option value="0">请选择</option>
                    {foreach name="typelist" item="vo"}
                    <option value="{$vo.id}" {if condition="$data.typeid eq $vo.id"}selected{/if} >{$vo.title}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属项目</label>
            <div class="layui-input-block layui-form-select">
                <input type="text" name="project" placeholder="" autocomplete="off" class="layui-input" value="{$data.project}" id="project">
                <dl class="layui-anim layui-anim-upbit" id="projectList">
                </dl>
            </div>
        </div>
        
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="test2">多图片上传</button>
                <input class="layui-upload-file" type="file" accept="" name="file" multiple=""> 
                <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                  预览图：
                  <div class="layui-upload-list" id="demo2"></div>
               </blockquote>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="submit">保存</button>
                <button type="button" id="uploads" style="display: none;"></button>
            </div>
        </div>
    </form>
</div>
{/block}
{block name="js"}
<script>
    var form = layui.form;
    form.on('select(parent_typeid)', function(res) { 
        $("#typeid").empty();
        if(res.value != 0){
            $.get("{:url('admin/DataType/getSonType')}?pid="+res.value,function(result){
                if(result.status){
                    $("#typeid").append(new Option('请选择',0));
                    $.each(result.data,function(index,item){
                        $("#typeid").append(new Option(item.title,item.id));
                    });
                    form.render("select");
                }
            });
        }else{
            $("#typeid").append(new Option('请选择',0));
            form.render("select");
        }
    })

    layui.use('upload', function(){
        var $ = layui.jquery,
        upload = layui.upload;
        upload.render({
            elem: '#test2'
            ,url: '{:request()->url()}' //改成您自己的上传接口
            ,auto: false //选择文件后不自动上传
            ,multiple: true
            ,accept: 'images'
            ,acceptMime: "image/*"
            ,exts: 'png|jpg|bmp' 
            ,size: 5000 
            ,field: 'pic'
            ,bindAction: '#uploads'
            ,data: {
                parent_typeid: function () {
                    return $("#parent_typeid").val();
                },
                typeid: function () {
                    return $("#typeid").val();
                },
                project: function () {
                    return $("#project").val();
                }
            }
            ,choose: function(obj){
                //FILES = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo2').append('<img id="'+index+'" src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                });
            }
            ,done: function(res, index, upload){
                //上传完毕
                if(res.status){
                    $('#'+index).css('opacity','0.4');
                }
                //delete FILES[index]; //删除队列
            }
            ,allDone: function(obj){
                //当文件全部被提交后，才触发
                layer.msg('全部执行完毕', {
                time: 1500 
                }, function(){
                    window.parent.location.reload();
                }); 
            }
            ,error: function(res){
                console.log(res);
            }
        });
   }); 

   $(function(){
       $('#project').keyup(function(event){
            var word = $("#project").val().trim();
            if(word.length>0){
                $.get("{:url('admin/DataProject/getProjectByLike')}",{word:word},function(res){
                    if(res.status){
                        var html = '';
                        $.each(res.data,function(index,obj){
                            html += '<dd data-tid="'+obj.typeid+'" data-ptid="'+obj.parent_typeid+'">'+obj.title+'</dd>';
                        });
                        $('#projectList').html(html).css('display','block');
                    }else{
                        $('#projectList').html('').css('display','none');
                    }
                });
            }else{
                $('#projectList').html('').css('display','none');
            }
       });

       $('#projectList').on('click','dd',function(){
           var ptid = $(this).attr('data-ptid');
           var tid = $(this).attr('data-tid');

           $('#project').val($(this).text());
           $('#projectList').html('').css('display','none');
           
           var dd = $('#parent_typeid').next().find('dl dd[lay-value='+ptid+']');
           dd.click();

            $.get("{:url('admin/DataType/getSonType')}?pid="+ptid,function(result){
                if(result.status){
                    $("#typeid").empty();
                    $("#typeid").append(new Option('请选择',0));
                    $.each(result.data,function(index,item){
                        if(tid == item.id){
                            $("#typeid").append(new Option(item.title,item.id,false,true));
                        }else{
                            $("#typeid").append(new Option(item.title,item.id));
                        }
                    });
                    form.render("select");
                }
            });
           
       });
       $('#project').click(function(){
            if( $("#projectList").children().length ){
                $("#projectList").toggle();
            }
       });

       $('#submit').click(function(){
            var word = $("#project").val().trim();
            if(word.length>0){
                $.get("{:url('admin/DataProject/getProjectByName')}",{word:word},function(res){
                    if(res.status){
                        $('#uploads').click();
                    }else{
                        layer.msg('此项目不存在',{time:1500,icon:2});
                    }
                });
            }else{
                $('#uploads').click();
            }
       });
   });
</script>
{/block}